import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.nio.file.Path;
import java.nio.file.Paths;

/**
 * The type Search recipe frame.
 *
 * @author Yihan Wang
 * @date 24/06/11
 */
public class SearchRecipeFrame extends JFrame implements ActionListener {
    /**
     * The Back.
     */
    JButton back = new JButton("Back");
    /**
     * The Search button.
     */
    JButton searchButton = new JButton("Search");
    /**
     * The Recipe input.
     */
    JTextField recipeInput = new JTextField(20);
    /**
     * The Result label.
     */
    JLabel resultLabel = new JLabel();
    /**
     * The Button panel.
     */
    JPanel buttonPanel = new JPanel();
    /**
     * The Panel.
     */
    JPanel panel = new JPanel();
    /**
     * The Background label.
     */
    JLabel backgroundLabel = new JLabel();


    /**
     * Instantiates a new Search recipe frame.
     */
    public SearchRecipeFrame() {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                Interface();
                element();
            }
        });
        this.setVisible(true);
    }

    /**
     * Interface.
     */
    public void Interface() {
        this.setTitle("Search food page");
        this.pack();
        this.setSize(new Dimension(1200, 675));
        this.setResizable(false);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLocationRelativeTo(null);
        this.getContentPane().add(buttonPanel);
        this.setLayout(new BorderLayout());

        Path filePath = Paths.get(System.getProperty("user.dir"), "resources/basicBackground.png");
        String basicBackgroundFilePath = filePath.toString();

        backgroundLabel = new JLabel(new ImageIcon(basicBackgroundFilePath));
        backgroundLabel.setBounds(0, 0, 1200, 675);
        backgroundLabel.setLayout(new BorderLayout());
        backgroundLabel.add(buttonPanel);
        backgroundLabel.setVisible(true);
        panel.add(backgroundLabel);
        this.getContentPane().add(panel);
    }

    /**
     * Element.
     */
    public void element() {
        back.setBounds(25, 25, 80, 80);
        back.setFont(new Font("Posterama", Font.PLAIN, 20));
        back.setBackground(Color.orange);
        back.addActionListener(this);

        recipeInput.setBounds(350, 100, 500, 40);
        recipeInput.setFont(new Font("Posterama", Font.PLAIN, 20));

        searchButton.setBounds(350, 150, 500, 40);
        searchButton.setFont(new Font("Posterama", Font.PLAIN, 20));
        searchButton.setBackground(Color.orange);
        searchButton.addActionListener(this);

        resultLabel.setBounds(350, 200, 500, 400);
        resultLabel.setFont(new Font("Posterama", Font.PLAIN, 16));
        resultLabel.setVerticalAlignment(SwingConstants.TOP);

        buttonPanel.setLayout(null);
        buttonPanel.setOpaque(false);
        buttonPanel.add(back);
        buttonPanel.add(recipeInput);
        buttonPanel.add(searchButton);
        buttonPanel.add(resultLabel);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        Object obj = e.getSource();

        if (obj == back) {
            this.setVisible(false);
            new MenuFrame();
        } else if (obj == searchButton) {
            String recipeName = recipeInput.getText().trim();
            Recipe recipe = Main.rc1.searchRecipe(recipeName);
            if (recipe != null) {
                resultLabel.setText("<html>" + recipe.printRecipeInfo().replace("\n", "<br>") + "</html>");
            } else {
                resultLabel.setText("Recipe not found.");
            }
        }
    }
}